
#include "rtthread.h"
#include "drv_clock.h"
#include "board.h"
#include <rtdevice.h>
#include <rtthread.h>
#include <rthw.h>

#include "iomem.h"
#include "drv_clock.h"
#include "drv_gpio.h"

#include "lcd_port.h"
#include "touch_port.h"

#define LCD_PGIO_LCD_SPI_CS  GPIO_PORT_A, GPIO_PIN_0
#define LCD_PGIO_LCD_SPI_CLK  GPIO_PORT_A, GPIO_PIN_1
#define LCD_PGIO_LCD_SPI_SDA  GPIO_PORT_A, GPIO_PIN_2


#define LCD_SPI_CS(a)	gpio_direction_output(LCD_PGIO_LCD_SPI_CS, a);hw_lcd_delay(1);

#define LCD_SPI_DCLK(a)	gpio_direction_output(LCD_PGIO_LCD_SPI_CLK, a);hw_lcd_delay(1);

#define LCD_SPI_SDA(a)	gpio_direction_output(LCD_PGIO_LCD_SPI_SDA, a);hw_lcd_delay(1);



void hw_lcd_delay(volatile int time)
{
	volatile int i;	
	while(time--)
		for(i=500;i>0;i--);
}

void LCD_WriteByteSPI(unsigned char byte)
{
    unsigned char n;
   
    for(n=0; n<8; n++)			
    {  
        if(byte&0x80) 
		{
			LCD_SPI_SDA(1);
		}
        else 
		{
			LCD_SPI_SDA(0);
		}

        byte<<= 1;
		hw_lcd_delay(1);
        LCD_SPI_DCLK(0);
		hw_lcd_delay(1);
        LCD_SPI_DCLK(1);
    }
}
void SPI_WriteComm(uint16_t CMD)
{			
	LCD_SPI_CS(0);
	LCD_SPI_SDA(0);
	LCD_SPI_DCLK(0);
	LCD_SPI_DCLK(1);
	LCD_WriteByteSPI(CMD);
	LCD_SPI_CS(1);
}
void SPI_WriteData(uint16_t tem_data)
{			
	LCD_SPI_CS(0);
	LCD_SPI_SDA(1);
	LCD_SPI_DCLK(0);
	LCD_SPI_DCLK(1);
	LCD_WriteByteSPI(tem_data);
	LCD_SPI_CS(1);
}


void lcd_cmd_tk050f5590_init(void)
{
	//****************************************************************************//
	//****************************** Page 1 Command ******************************//
	//****************************************************************************//
	SPI_WriteComm(0xFF); // Change to Page 1
	SPI_WriteData(0xFF); 
	SPI_WriteData(0x98); 
	SPI_WriteData(0x06);
	SPI_WriteData(0x04); 
	SPI_WriteData(0x01);

	SPI_WriteComm(0X08);SPI_WriteData(0X10);                 // output SDA

	SPI_WriteComm(0X21);SPI_WriteData(0X01);                 // DE = 1 Active

	SPI_WriteComm(0X30);SPI_WriteData(0X01);                 // 480 X 854

	SPI_WriteComm(0X31);SPI_WriteData(0X02);                 // 2-dot Inversion

	SPI_WriteComm(0X40);SPI_WriteData(0X18);                // DDVDH/L BT 17 -10 5.94 -5.47 -2.3 1.6 43.4 21.88 0.17

	SPI_WriteComm(0X41);SPI_WriteData(0X66);              // DDVDH/L CLAMP 

	SPI_WriteComm(0X42);SPI_WriteData(0X02);                 // VGH/VGL 

	SPI_WriteComm(0X43);SPI_WriteData(0X0A);                 // VGH/VGL 

	SPI_WriteComm(0X50);SPI_WriteData(0X78);                 // VGMP 4.6 

	SPI_WriteComm(0X51);SPI_WriteData(0X78);                 // VGMN 4.6 

	SPI_WriteComm(0X52);SPI_WriteData(0X00);                 // Flicker 

	SPI_WriteComm(0X53);SPI_WriteData(0X1F);                 // Flicker 

	SPI_WriteComm(0X57);SPI_WriteData(0X50);

	SPI_WriteComm(0X60);SPI_WriteData(0X07);                 // SDTI

	SPI_WriteComm(0X61);SPI_WriteData(0X01);                 // CRTI 00

	SPI_WriteComm(0X62);SPI_WriteData(0X07);                 // EQTI 07

	SPI_WriteComm(0X63);SPI_WriteData(0X00);                 // PCTI  00


	////++++++++++++++++++ Gamma Setting ++++++++++++++++++//
	//SPI_WriteComm(0xFF); // Change to Page 1
	//SPI_WriteData(0xFF); 
	//SPI_WriteData(0x98); 
	//SPI_WriteData(0x06);
	//SPI_WriteData(0x04); 
	//SPI_WriteData(0x01);

	SPI_WriteComm(0XA0);SPI_WriteData(0X00);  // Gamma 0 

	SPI_WriteComm(0XA1);SPI_WriteData(0X0A);  // Gamma 4 

	SPI_WriteComm(0XA2);SPI_WriteData(0X12);  // Gamma 8

	SPI_WriteComm(0XA3);SPI_WriteData(0X0D);  // Gamma 16

	SPI_WriteComm(0XA4);SPI_WriteData(0X07);  // Gamma 24

	SPI_WriteComm(0XA5);SPI_WriteData(0X09);  // Gamma 52

	SPI_WriteComm(0XA6);SPI_WriteData(0X06); // Gamma 80

	SPI_WriteComm(0XA7);SPI_WriteData(0X03);  // Gamma 108

	SPI_WriteComm(0XA8);SPI_WriteData(0X09);  // Gamma 147

	SPI_WriteComm(0XA9);SPI_WriteData(0X0D);  // Gamma 175

	SPI_WriteComm(0XAA);SPI_WriteData(0X15);  // Gamma 203

	SPI_WriteComm(0XAB);SPI_WriteData(0X07);  // Gamma 231

	SPI_WriteComm(0XAC);SPI_WriteData(0X0D);  // Gamma 239

	SPI_WriteComm(0XAD);SPI_WriteData(0X0D);  // Gamma 247

	SPI_WriteComm(0XAE);SPI_WriteData(0X06);  // Gamma 251

	SPI_WriteComm(0XAF);SPI_WriteData(0X00);  // Gamma 255

	///==============Nagitive
	SPI_WriteComm(0XC0);SPI_WriteData(0X00);  // Gamma 0 

	SPI_WriteComm(0XC1);SPI_WriteData(0X0A);  // Gamma 4

	SPI_WriteComm(0XC2);SPI_WriteData(0X12);  // Gamma 8

	SPI_WriteComm(0XC3);SPI_WriteData(0X0E);  // Gamma 16

	SPI_WriteComm(0XC4);SPI_WriteData(0X07);  // Gamma 24

	SPI_WriteComm(0XC5);SPI_WriteData(0X09);  // Gamma 52

	SPI_WriteComm(0XC6);SPI_WriteData(0X07);  // Gamma 80

	SPI_WriteComm(0XC7);SPI_WriteData(0X03);  // Gamma 108

	SPI_WriteComm(0XC8);SPI_WriteData(0X09);  // Gamma 147

	SPI_WriteComm(0XC9);SPI_WriteData(0X0C);  // Gamma 175

	SPI_WriteComm(0XCA);SPI_WriteData(0X15);  // Gamma 203

	SPI_WriteComm(0XCB);SPI_WriteData(0X07);  // Gamma 231

	SPI_WriteComm(0XCC);SPI_WriteData(0X0C);  // Gamma 239

	SPI_WriteComm(0XCD);SPI_WriteData(0X0D); // Gamma 247

	SPI_WriteComm(0XCE);SPI_WriteData(0X06); // Gamma 251

	SPI_WriteComm(0XCF);SPI_WriteData(0X00);  // Gamma 255


	//****************************************************************************//
	//****************************** Page 6 Command ******************************//
	//****************************************************************************//
	SPI_WriteComm(0xFF); // Change to Page 6
	SPI_WriteData(0xFF); 
	SPI_WriteData(0x98); 
	SPI_WriteData(0x06);
	SPI_WriteData(0x04); 
	SPI_WriteData(0x06);

	SPI_WriteComm(0X00);SPI_WriteData(0X21);

	SPI_WriteComm(0X01);SPI_WriteData(0X06);

	SPI_WriteComm(0X02);SPI_WriteData(0XA0 );   

	SPI_WriteComm(0X03);SPI_WriteData(0X02);

	SPI_WriteComm(0X04);SPI_WriteData(0X01);

	SPI_WriteComm(0X05);SPI_WriteData(0X01);

	SPI_WriteComm(0X06);SPI_WriteData(0X80);    

	SPI_WriteComm(0X07);SPI_WriteData(0X04);

	SPI_WriteComm(0X08);SPI_WriteData(0X00);

	SPI_WriteComm(0X09);SPI_WriteData(0X80);  

	SPI_WriteComm(0X0A);SPI_WriteData(0X00);   

	SPI_WriteComm(0X0B);SPI_WriteData(0X00);    

	SPI_WriteComm(0X0C);SPI_WriteData(0X2c);

	SPI_WriteComm(0X0D);SPI_WriteData(0X2c);

	SPI_WriteComm(0X0E);SPI_WriteData(0X1c);

	SPI_WriteComm(0X0F);SPI_WriteData(0X00);

	SPI_WriteComm(0X10);SPI_WriteData(0XFF);

	SPI_WriteComm(0X11);SPI_WriteData(0XF0);

	SPI_WriteComm(0X12);SPI_WriteData(0X00);

	SPI_WriteComm(0X13);SPI_WriteData(0X00);

	SPI_WriteComm(0X14);SPI_WriteData(0X00);

	SPI_WriteComm(0X15);SPI_WriteData(0XC0);

	SPI_WriteComm(0X16);SPI_WriteData(0X08);

	SPI_WriteComm(0X17);SPI_WriteData(0X00);

	SPI_WriteComm(0X18);SPI_WriteData(0X00);

	SPI_WriteComm(0X19);SPI_WriteData(0X00);

	SPI_WriteComm(0X1A);SPI_WriteData(0X00);

	SPI_WriteComm(0X1B);SPI_WriteData(0X00);

	SPI_WriteComm(0X1C);SPI_WriteData(0X00);

	SPI_WriteComm(0X1D);SPI_WriteData(0X00);


	SPI_WriteComm(0X20);SPI_WriteData(0X01);

	SPI_WriteComm(0X21);SPI_WriteData(0X23);

	SPI_WriteComm(0X22);SPI_WriteData(0X45);

	SPI_WriteComm(0X23);SPI_WriteData(0X67);

	SPI_WriteComm(0X24);SPI_WriteData(0X01);

	SPI_WriteComm(0X25);SPI_WriteData(0X23);

	SPI_WriteComm(0X26);SPI_WriteData(0X45);

	SPI_WriteComm(0X27);SPI_WriteData(0X67);


	SPI_WriteComm(0X30);SPI_WriteData(0X12);

	SPI_WriteComm(0X31);SPI_WriteData(0X22);

	SPI_WriteComm(0X32);SPI_WriteData(0X22);

	SPI_WriteComm(0X33);SPI_WriteData(0X22);

	SPI_WriteComm(0X34);SPI_WriteData(0X87);

	SPI_WriteComm(0X35);SPI_WriteData(0X96);

	SPI_WriteComm(0X36);SPI_WriteData(0XAA);

	SPI_WriteComm(0X37);SPI_WriteData(0XDB);

	SPI_WriteComm(0X38);SPI_WriteData(0XCC);

	SPI_WriteComm(0X39);SPI_WriteData(0XBD);

	SPI_WriteComm(0X3A);SPI_WriteData(0X78);

	SPI_WriteComm(0X3B);SPI_WriteData(0X69);

	SPI_WriteComm(0X3C);SPI_WriteData(0X22);

	SPI_WriteComm(0X3D);SPI_WriteData(0X22);

	SPI_WriteComm(0X3E);SPI_WriteData(0X22);

	SPI_WriteComm(0X3F);SPI_WriteData(0X22);

	SPI_WriteComm(0X40);SPI_WriteData(0X22);

	SPI_WriteComm(0X52);SPI_WriteData(0X10);

	SPI_WriteComm(0X53);SPI_WriteData(0X10);


	//SPI_WriteComm(0xFF); // Change to Page 7
	//SPI_WriteData(0xFF); 
	//SPI_WriteData(0x98); 
	//SPI_WriteData(0x06);
	//SPI_WriteData(0x04); 
	//SPI_WriteData(0x07);

	////SPI_WriteComm(0X17);SPI_WriteData(0X22);

	//SPI_WriteComm(0X18);SPI_WriteData(0X1D);

	////SPI_WriteComm(0X02);SPI_WriteData(0X77);

	////SPI_WriteComm(0XE1);SPI_WriteData(0X79);

	////SPI_WriteComm(0X06);SPI_WriteData(0X01);


	//****************************************************************************//
	SPI_WriteComm(0xFF); // Change to Page 0
	SPI_WriteData(0xFF); 
	SPI_WriteData(0x98); 
	SPI_WriteData(0x06);
	SPI_WriteData(0x04); 
	SPI_WriteData(0x00);

	//SPI_WriteComm(0X36);SPI_WriteData(0X0A);
//	SPI_WriteComm(0X3A);SPI_WriteData(0X77);
	
	//SPI_WriteComm(0X36);SPI_WriteData(0XA3);
	
//	SPI_WriteComm(0X36);SPI_WriteData(0X00);
//	SPI_WriteComm(0X3A);SPI_WriteData(0X55);
	
	SPI_WriteComm(0X36);SPI_WriteData(0X08);
	SPI_WriteComm(0X3A);SPI_WriteData(0X77);

	SPI_WriteComm(0X11);              // Sleep-Out
	hw_lcd_delay(1000);
	SPI_WriteComm(0X29);               // Display On
}


int lcd_cmd_init(void)
{
	
    gpio_set_func(LCD_PGIO_LCD_SPI_CS, IO_OUTPUT);
    gpio_direction_output(LCD_PGIO_LCD_SPI_CS, 0);

    gpio_set_func(LCD_PGIO_LCD_SPI_CLK, IO_OUTPUT);
    gpio_direction_output(LCD_PGIO_LCD_SPI_CLK, 0);

    gpio_set_func(LCD_PGIO_LCD_SPI_SDA, IO_OUTPUT);
    gpio_direction_output(LCD_PGIO_LCD_SPI_SDA, 0);

	hw_lcd_delay(100);

	lcd_cmd_tk050f5590_init();

	hw_lcd_delay(100);

    gpio_set_func(LCD_PGIO_LCD_SPI_CS, IO_OUTPUT);
    gpio_direction_output(LCD_PGIO_LCD_SPI_CS, 1);

    gpio_set_func(LCD_PGIO_LCD_SPI_CLK, IO_OUTPUT);
    gpio_direction_output(LCD_PGIO_LCD_SPI_CLK, 1);

    gpio_set_func(LCD_PGIO_LCD_SPI_SDA, IO_INPUT);
    gpio_direction_input(LCD_PGIO_LCD_SPI_SDA);
	return 0;
}
